﻿<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加商品</title>
    <link rel="stylesheet" href="<%=basePath %>web/css/from.css">
    <link rel="stylesheet" href="<%=basePath %>web/themes/default/easyui.css">
    <link rel="stylesheet" href="<%=basePath %>web/themes/icon.css">
    <script src="<%=basePath %>web/js/jquery.min.js"></script>
    <script src="<%=basePath %>web/js/jquery.easyui.min.js"></script>
    <script src="<%=basePath %>web/js/easyui-lang-zh_CN.js"></script>
    <script>
        //订单号
            $(function(){
          	 $.ajax({
        		url:"/kxmanage/osb2/code?prefix=IQ",
        		type:"post",
        		dataType:"json",
        		success:function(data){
        			$("#code").textbox('setValue',data.result)
        		}
        	})
        	var newdate=new Date();
        	$("#addDate").datetimebox("setValue",myformatter(newdate))
        	})
        //日期格式化
         function myformatter(date){
          var y=date.getFullYear();
          var m=date.getMonth()+1;
          var d=date.getDate();
          var hh=date.getHours();
          var mm=date.getMinutes();
          var ss=date.getSeconds();
          return y+"-"+(m<10?('0'+m):m)+"-"+(d<10?('0'+d):d)+" "+(hh<10?('0'+hh):hh)+":"+(mm<10?('0'+mm):mm)+":"+(ss<10?('0'+ss):ss);
        }
        
        	
        //添加供应商
        function loadSupplier() {
            $("#list").dialog({
                title: '供应商列表',
                width: 905,
                height: 400,
                closed: false,
                cache: false,
                modal: true
            });

        }
 //关闭当前页
    	   function closeTabs(){
			parent.closeTabs("添加产品");
		  }    
        
        
        //用于将子页面的数据绑定到相关元素上
        function bindValue(data){
        	var name=data[0];
        	var linkman=data[1];
        	var phone=data[2];
        	var fax=data[3];
        	var address=data[4];
        	$('#supplierName').textbox('setValue',name);	
        	$('#contacter').textbox('setValue',linkman);	
        	$('#phone').textbox('setValue',phone);	
        	$('#fax').textbox('setValue',fax);	
        }
        //销毁
         function closeSon(){
        	$("#list").dialog("close");
	 
        }
     //添加配件
     function loadPart() {
        var supplierName=$("#supplierName").textbox("getValue")
        if(supplierName!=null&&supplierName!=''){
        	$("#partList").dialog({
                title: '选择配件',
                width: 1000,
                height: 400,
                closed: false,
                cache: false,
                modal: true,
           })
        }else{
        	alert("请选择客户名称")
        }
        }  
 //添加信息
       $(function(){
 
        $("#generatingParts").datagrid({       
                checkOnSelect:true,
                rownumbers:true,                   
                columns:[[                    
                    {field:'code',title:'配件号',width:150},
                    {field:'partName',title:'配件名称',width:173},
                    {field:'partBrand',title:'配件品牌',width:110},
                    {field:'partNo',title:'配件型号',width:100},
                    {field:'amount',title:'数量',width:80,
                       formatter:function(value,row,index){
                    if(row.avg==null){
                    		return value;
                    	}
                            return '<input name="amount" onchange="change_amount_sellPrice('+index+',this)" type="number" value="'+value+'" style="width:30px;"/>';
                        }
                    
                    },
                    {field:'avg',title:'单价(￥)',width:80,
                         formatter:function(value,row,index){
                            if(row.avg==null){
                    		return null;
                    	    }
                            return '<input name="avg" onchange="change_amount_sellPrice('+index+',this)" type="number" value="'+value+'" style="width:50px;"/>';
                         }
                    },
                    {field:'price',title:'金额(￥)',width:80},
                    {field:'memo',title:'备注',width:180},
                   	{field:'opt',title:'操作',width:92,
                   	    formatter:function (value,row,index) {
                            if(row.avg==null){
    	                		return null;
	                    	}
                            var context="<a  href=\"javascript:del('"+index+"')\">删除</a>&nbsp;&nbsp;"
                            return context;
                          }
                        }
                	]],
                	onClickRow:function(index,data){
                	$(this).datagrid('unselectRow',index);
                }
        	});    
        	   
        });
 //父类的addpart    
        function closePart() {
            $("#partList").dialog("close");
        };   
        
     
            //用于将子页面的数据绑定到相关元素上
        function addpart(row){
            var flag=true;
            var x=true;
            var oldRow=$('#generatingParts').datagrid('getRows');
            
            for(var i=0;i<row.length;i++){
               if(oldRow!=null&&oldRow!=''){
                 for(var j=0;j<oldRow.length;j++){
                   if(oldRow[j]['code']==row[i]['code']){
         		       alert("不能添加重复配件");
         		       flag=false;
         		       break;
         		   }
                 } 
                 if(flag==true){
                 }else{
                 	break;
                 }
                }else{
                	addToPage(row[i]);
                	x=false;
                }
             }
             if(flag==true){
             	if(x==true){
                 	for(var i=0;i<row.length;i++){
			         $('#generatingParts').datagrid('deleteRow',oldRow.length-1); 
                 	 addToPage(row[i]);
					}
                }else{
                 	for(var i=1;i<row.length;i++){
			         $('#generatingParts').datagrid('deleteRow',oldRow.length-1); 
                 	 addToPage(row[i]);
					}	
                }
              }
	              closePart();
         }

        
        function addToPage(row){
           $('#generatingParts').datagrid('appendRow',{
				code: row.code,
				partName: row.partName,
				partBrand: row.partBrand,
				partNo: row.partNo,
				amount: 1,
				avg: row.sellPrice,
				price: row.sellPrice,
				memo: row.memo,
				opt: row.opt
		   });		 
		   $("#generatingParts").datagrid('appendRow',{
                	  'partBrand':"合计",
                	  'amount':total('amount'),
                	  'price':total('price')
          });
		     
        }   
//合计
        function total(col){
        	var rows=$("#generatingParts").datagrid('getRows');
        	var total=0;
        	for(var i=0;i<rows.length;i++){
        		total+=parseFloat(rows[i][col]);
        	}
        	return total;
          };
 //保存  
   $(function(){
     $("#baocun").click(function(){
     //alert("DS")	
     var ms=[];
     
     var form=$("#form").serializeArray();
     //alert(JSON.stringify(form))
	 var oldRow=$('#generatingParts').datagrid('getRows');
	 var data =JSON.stringify($('#generatingParts').datagrid('getData').rows);
    $(".partmemo").each(function(){
          ms.push($(this).val());
        		});
       var mm=JSON.stringify(ms)
     
     if(oldRow==null||oldRow==''){
     
        alert("没有添加配件信息")
        	}else{
        	var money=parseFloat(oldRow[oldRow.length-1]['price']);
        	// alert(parseFloat(oldRow[oldRow.length-1]));        	 
        	  $.ajax({
        		type:"post",
        		url:"/kxmanage/IBC/baocun?data="+data+"&money="+money+"&mm="+mm,
        		data:form,
        		dataType:"json",
        		success:function(data){
        			if(data.result=="success"){
        				alert("添加成功"); 
        					parent.addTabs("产品列表","goods/model.jsp");
							closeTabs();      				
        			}else{
        				alert("添加错误");
        			}
        		}
        		
        		});
        	
      }  
      
 });
 
 });
 //
     function change_amount_sellPrice(input_index,input){
        
         	var amount=$(input).parent().parent().parent().find('[name="amount"]').val();
         	var avg=$(input).parent().parent().parent().find('[name="avg"]').val();
         	var price=parseFloat(amount)*parseFloat(avg);
         	$('#generatingParts').datagrid('updateRow',{
				index: input_index,
				row: {
					amount: amount,
					avg:avg,
					price: price
				}
			});
			var oldRow=$('#generatingParts').datagrid('getRows');
			$('#generatingParts').datagrid('deleteRow',oldRow.length-1); 
			$("#generatingParts").datagrid('appendRow',{
                	  'partBrand':"合计",
                	  'amount':total('amount'),
                	  'price':total('price')
           	});
         }
   //
 $(function(){
          $("#guanbixun").click(function(){
          
           parent.addTabs("产品列表","goods/model.jsp");
			closeTabs();  
          
          
          })
 
 });
 //
          function del(index){
         	//删除当前行
         	$('#generatingParts').datagrid('deleteRow',index);
         	//拿到之前的数据并重新封装，刷新index
         	var oldData=$('#generatingParts').datagrid('getData');
         	$('#generatingParts').datagrid('loadData',oldData);
         	//更新最后一行
         	var oldRow=$('#generatingParts').datagrid('getRows');
         	$('#generatingParts').datagrid('deleteRow',oldRow.length-1); 
			$("#generatingParts").datagrid('appendRow',{
                	  'partBrand':"合计",
                	  'amount':total('amount'),
                	  'price':total('price')
           	});
         }
    </script>
    <style>
        body {
            font-size:12px;
            padding:10px
        }
        .l-table-edit {
            border:#84a0c4 1px solid
        }
        .l-table-edit-td {
            padding:4px;
            font-size:12px;
        }
         -->
    </style>


</head>
<body>


<form name="form"  id="form" action="" method="post">
<input type="hidden" id="outcode" value="${oB.code}">
    <table cellpadding="0"  cellspacing="0" class="l-table-edit" style="width:100%"  >
        <tr >
            <td width="17%"  align="right" class="l-table-edit-td">询价单号：</td>
            <td width="27%" align="left" class="l-table-edit-td"><input class="easyui-textbox" name="code" type="text" id="code" value="${oB.code}" size="30" maxlength="100" ltype="text" readonly="readonly"/></td>
            <td width="13%"  align="right" class="l-table-edit-td">询价日期：</td>
            <td width="43%" align="left" class="l-table-edit-td"><input class="easyui-datetimebox" name="inquiryTime"  id="addDate" value='<fmt:formatDate value="${oB.outstorageDate }" pattern="yyyy-MM-dd hh:mm:ss" />' style="width:168px" "/></td>
        </tr>
        <tr>
            <td width="17%" align="right" class="l-table-edit-td">
                <a href="javascript:loadSupplier();">供应商名称</a>：
            </td>
            <td align="left" class="l-table-edit-td">
                <input class="easyui-textbox" name="supplierName" type="text" id="supplierName"  value=""  maxlength="100" ltype="text"  style="width:239px" readonly="readonly"/>
            <td width="13%" align="right" class="l-table-edit-td">联系人员：</td>
            <td align="left" class="l-table-edit-td">
                <input class="easyui-textbox" name="linkman" type="text" id="contacter" value="" maxlength="30" ltype="text" readonly="readonly"/></td>
        </tr>
        <tr>
            <td width="17%" align="right" class="l-table-edit-td">电&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;话：</td>
            <td align="left" class="l-table-edit-td"><input class="easyui-textbox" name="phone"  type="text" id="phone" value="" size="30" maxlength="50" ltype="text" readonly="readonly"/></td>
            <td width="17%" align="right" class="l-table-edit-td">传真：</td>
            <td align="left" class="l-table-edit-td"><input class="easyui-textbox" type="text" id="fax" value="" size="30" maxlength="1000" ltype="text" readonly="readonly"/></td>                
        </tr>
        <tr>
            <td width="17%" align="right" class="l-table-edit-td">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注：</td>
            <td align="left" class="l-table-edit-td"><input  name="memo" class="easyui-textbox" type="text" id="type" value="" size="30"  style="width:500px" ltype="text" /></td>            
        </tr>
        
    </table>
</form>
<br>
<a href="javascript:loadPart()"><input  type="button" value="添加配件" style="width:60px" ></a>
<input  type="button" id="baocun" value="保存" style="width:60px" >
<input  type="button" value="打印" style="width:60px" disabled="disabled">
<input  type="button" value="生成word" style="width:60px" disabled="disabled">
<input  type="button"  id="guanbixun" value="关闭" style="width:60px">

<div id="generatingParts" class="easyui-datagrid">
</div>
<div id="absolute">
<div class="easyui-datagrid" id="datatable"></div>
<div id="list"    data-options="onClose:function(){closeSon()}" style="display: none">
    <iframe src='<%=basePath %>web/goods/company2.jsp' frameborder='0'style='width: 100%;height: 100%'></iframe>
</div>
</div>
<div id="partList"  style="display: none">
    <iframe src='<%=basePath %>web/goods/addPart2.jsp' frameborder='0'style='width: 100%;height: 100%'></iframe>
</div>
</body>
</html>